package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.stock2.domain.STOCK;
import com.ruoyi.stock2.thread.aboutDownloadData.HqSinajsServiceWithOutRedis;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        LocalDateTime date2 = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String datestr = date2.format(formatter);
        if(datestr.compareTo("11:30:00")>0 && datestr.compareTo("11:59:59")<0){
            return;
        }

        System.out.println("执行有参方法：" + params);
        StringBuilder tempString = new StringBuilder();

        for (String Symbol : params.split("_")) {

            if(Symbol.startsWith("0")){
                tempString.append("sz"+Symbol).append(",");
            }

            if(Symbol.startsWith("3")){
                tempString.append("sz"+Symbol).append(",");
            }

            if(Symbol.startsWith("6")){
                tempString.append("sh"+Symbol).append(",");
            }
        }
        hqsinajs(tempString.toString());


    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    private void hqsinajs(String tempString) {
        Document doc;
        try {
            doc = Jsoup.connect("http://hq.sinajs.cn/list=" + tempString).ignoreContentType(true).get();
            String text = doc.body().text();
            String hq[] = text.split(";");
            for (String s : hq) {
                // hq_str_sz000006="深振业Ａ,7.330,7.300,7.240,7.440,7.120,7.230,7.240,7879672,57568150.670,15500,7.230,3000,7.220,22400,7.210,174200,7.200,19100,7.190,25800,7.240,27000,7.250,14000,7.260,15100,7.270,25000,7.280,2016-05-13,16:30:03,00";
                String oneSymbolHq[] = s.replaceAll("\"", "").replaceAll(" ", "").split("=");
                if (oneSymbolHq.length == 1) {

                } else {

                    String nameAndPrice[] = oneSymbolHq[1].split(",");
                    // String name = nameAndPrice[0]; 如：  深振业Ａ
                    BigDecimal oldprice = new BigDecimal(nameAndPrice[2]);
                    BigDecimal hprice = new BigDecimal(nameAndPrice[4]);
                    BigDecimal lprice = new BigDecimal(nameAndPrice[5]);
                    BigDecimal price = new BigDecimal(nameAndPrice[3]);
                    BigDecimal sal5 = new BigDecimal(nameAndPrice[28]);
                    BigDecimal sal5p = new BigDecimal(nameAndPrice[29]);
                    BigDecimal sal4 = new BigDecimal(nameAndPrice[26]);
                    BigDecimal sal4p = new BigDecimal(nameAndPrice[27]);
                    BigDecimal sal3 = new BigDecimal(nameAndPrice[24]);
                    BigDecimal sal3p = new BigDecimal(nameAndPrice[25]);
                    BigDecimal sal2 = new BigDecimal(nameAndPrice[22]);
                    BigDecimal sal2p = new BigDecimal(nameAndPrice[23]);
                    BigDecimal sal1 = new BigDecimal(nameAndPrice[20]);
                    BigDecimal sal1p = new BigDecimal(nameAndPrice[21]);
                    BigDecimal buy1 = new BigDecimal(nameAndPrice[10]);
                    BigDecimal buy1p = new BigDecimal(nameAndPrice[11]);
                    BigDecimal buy2 = new BigDecimal(nameAndPrice[12]);
                    BigDecimal buy2p = new BigDecimal(nameAndPrice[13]);
                    BigDecimal buy3 = new BigDecimal(nameAndPrice[14]);
                    BigDecimal buy3p = new BigDecimal(nameAndPrice[15]);
                    BigDecimal buy4 = new BigDecimal(nameAndPrice[16]);
                    BigDecimal buy4p = new BigDecimal(nameAndPrice[17]);
                    BigDecimal buy5 = new BigDecimal(nameAndPrice[18]);
                    BigDecimal buy5p = new BigDecimal(nameAndPrice[19]);
                    BigDecimal zhangfu = new BigDecimal(0);

                    String symbol = oneSymbolHq[0].substring(12); //yyyyMMddHHmmss

                    if(oldprice.compareTo(new BigDecimal(0.0))!=0){
                        zhangfu = price.subtract(oldprice).divide(oldprice, 4, BigDecimal.ROUND_HALF_UP)
                                .multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                    }

                    String dat = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                    System.out.println(dat + ": " + symbol + ", zf:"+ zhangfu + ", old:" + oldprice + ", now:"  + price + ", h:" + hprice + ", l:" + lprice);
                    System.out.println("s "+sal5p + "--" + sal5.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)" );
                    System.out.println("s "+sal4p + "--" + sal4.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)" );
                    System.out.println("s "+sal3p + "--" + sal3.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)" );
                    System.out.println("s "+sal2p + "--" + sal2.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)"  );
                    System.out.println("s "+sal1p + "--" + sal1.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)" );
                    System.out.println("b "+buy1p + "--" + buy1.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)" );
                    System.out.println("b "+buy2p + "--" + buy2.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)" );
                    System.out.println("b "+buy3p + "--" + buy3.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)" );
                    System.out.println("b "+buy4p + "--" + buy4.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)"  );
                    System.out.println("b "+buy5p + "--" + buy5.divide(new BigDecimal(100), 0, BigDecimal.ROUND_HALF_UP) + " at java.lang.Thread.run(Unknown Source)"  );
                    System.out.println();

                }

            }

        } catch (java.net.SocketTimeoutException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        LocalDateTime date2 = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String datestr = date2.format(formatter);
        if(datestr.compareTo("11:30:00")>0 && datestr.compareTo("13:59:59")<0){
            System.out.println(111);
        }else {
            System.out.println(222);
        }

    }
}
